#include <qqthostaddressmanager.h>

#include <qqtframe.h>

QQtHostAddressManager::QQtHostAddressManager ( QObject* parent )
    : QObject ( parent )
{
    refreshAddressDictionary();
}

QQtHostAddressManager::~QQtHostAddressManager() {}

void QQtHostAddressManager::refreshAddressDictionary()
{
    QString domainName = QHostInfo::localDomainName();
    QHostInfo domainHostInfo = QHostInfo::fromName ( domainName );
    //p3line() << domainHostInfo.addresses();
    mDictAddress["domain"]["domainName"] = domainName;
    QListIterator<QHostAddress> itor0 ( domainHostInfo.addresses() );
    while ( itor0.hasNext() )
    {
        const QHostAddress& address = itor0.next();

        if ( address.protocol() == QAbstractSocket::IPv4Protocol )
        {
            //qDebug() << address.toString();
            mDictAddress["domain"]["HostAddress"]["IPV4"] = address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::IPv6Protocol )
        {
            mDictAddress["domain"]["HostAddress"]["IPV6"] = address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::AnyIPProtocol )
        {
            mDictAddress["domain"]["HostAddress"]["Any"] = address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol )
        {
            mDictAddress["domain"]["HostAddress"]["Unknow"] = address.toString();
        }
    }

    QString hostName = QHostInfo::localHostName();
    QHostInfo hostInfo = QHostInfo::fromName ( hostName );
    //p3line() << hostInfo.addresses();
    mDictAddress["host"]["hostName"] = hostName;

    QListIterator<QHostAddress> itor ( hostInfo.addresses() );
    while ( itor.hasNext() )
    {
        const QHostAddress& address = itor.next();

        if ( address.protocol() == QAbstractSocket::IPv4Protocol )
        {
            //qDebug() << address.toString();
            mDictAddress["host"]["HostAddress"]["IPV4"] = address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::IPv6Protocol )
        {
            mDictAddress["host"]["HostAddress"]["IPV6"] = address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::AnyIPProtocol )
        {
            mDictAddress["host"]["HostAddress"]["Any"] = address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol )
        {
            mDictAddress["host"]["HostAddress"]["Unknow"] = address.toString();
        }
    }

    QList<QHostAddress> list1 = QNetworkInterface::allAddresses();
    int i = 0;
    QListIterator<QHostAddress> itor1 ( list1 );
    while ( itor1.hasNext() )
    {
        const QHostAddress& address = itor1.next();

        if ( address.protocol() == QAbstractSocket::IPv4Protocol )
        {
            //qDebug() << address.toString();
            mDictAddress["summary"][i] = "IPV4," + address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::IPv6Protocol )
        {
            mDictAddress["summary"][i] = "IPV6," + address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::AnyIPProtocol )
        {
            mDictAddress["summary"][i] = "Any," + address.toString();
        }
        else if ( address.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol )
        {
            mDictAddress["summary"][i] = "Unknow," + address.toString();
        }
        i++;
    }

    //获取所有网络接口的列表
    QList<QNetworkInterface> list2 = QNetworkInterface::allInterfaces();

    //遍历每一个网络接口
    i = 0;
    QListIterator<QNetworkInterface> itor2 ( list2 );
    while ( itor2.hasNext() )
    {
        const QNetworkInterface& interface1 = itor2.next();

        //设备名
        //qDebug() << "Device: " << interface1.name();
        mDictAddress["device"][i]["Device"] = interface1.name();
        //硬件地址
        //qDebug() << "HardwareAddress: " << interface1.hardwareAddress();
        mDictAddress["device"][i]["HardwareAddress"] = interface1.hardwareAddress();
        //获取IP地址条目列表，每个条目中包含一个IP地址，一个子网掩码和一个广播地址
        QList<QNetworkAddressEntry> entryList = interface1.addressEntries();
        //遍历每一个IP地址条目
        foreach ( QNetworkAddressEntry entry, entryList )
        {
            QHostAddress address = entry.ip();
            if ( address.protocol() == QAbstractSocket::IPv4Protocol )
            {
                //qDebug() << address.toString();

                //IP地址
                //qDebug() << "IP Address: " << entry.ip().toString();
                mDictAddress["device"][i]["HostAddress"]["IPV4"]["IP Address"] = entry.ip().toString();

                //子网掩码
                //qDebug() << "Netmask: " << entry.netmask().toString();
                mDictAddress["device"][i]["HostAddress"]["IPV4"]["Netmask"] = entry.netmask().toString();

                //广播地址
                //qDebug() << "Broadcast: " << entry.broadcast().toString();
                mDictAddress["device"][i]["HostAddress"]["IPV4"]["Broadcast"] = entry.broadcast().toString();
            }
            else if ( address.protocol() == QAbstractSocket::IPv6Protocol )
            {

                //IP地址
                //qDebug() << "IP Address: " << entry.ip().toString();
                mDictAddress["device"][i]["HostAddress"]["IPV6"]["IP Address"] = entry.ip().toString();

                //子网掩码
                //qDebug() << "Netmask: " << entry.netmask().toString();
                mDictAddress["device"][i]["HostAddress"]["IPV6"]["Netmask"] = entry.netmask().toString();

                //广播地址
                //qDebug() << "Broadcast: " << entry.broadcast().toString();
                mDictAddress["device"][i]["HostAddress"]["IPV6"]["Broadcast"] = entry.broadcast().toString();
            }
            else if ( address.protocol() == QAbstractSocket::AnyIPProtocol )
            {
                //IP地址
                //qDebug() << "IP Address: " << entry.ip().toString();
                mDictAddress["device"][i]["HostAddress"]["Any"]["IP Address"] = entry.ip().toString();

                //子网掩码
                //qDebug() << "Netmask: " << entry.netmask().toString();
                mDictAddress["device"][i]["HostAddress"]["Any"]["Netmask"] = entry.netmask().toString();

                //广播地址
                //qDebug() << "Broadcast: " << entry.broadcast().toString();
                mDictAddress["device"][i]["HostAddress"]["Unknown"]["Broadcast"] = entry.broadcast().toString();
            }
            else if ( address.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol )
            {
                //IP地址
                //qDebug() << "IP Address: " << entry.ip().toString();
                mDictAddress["device"][i]["HostAddress"]["Unknown"]["IP Address"] = entry.ip().toString();

                //子网掩码
                //qDebug() << "Netmask: " << entry.netmask().toString();
                mDictAddress["device"][i]["HostAddress"]["Unknown"]["Netmask"] = entry.netmask().toString();

                //广播地址
                //qDebug() << "Broadcast: " << entry.broadcast().toString();
                mDictAddress["device"][i]["HostAddress"]["Unknown"]["Broadcast"] = entry.broadcast().toString();
            }

        }

        i++;
    }
}

QQtDictionary& QQtHostAddressManager::dictionary()
{
    return mDictAddress;
}

QString QQtHostAddressManager::getDomainName()
{
    return QHostInfo::localDomainName();
}

QString QQtHostAddressManager::getDomainAddress()
{
    QString ip = mDictAddress["domain"]["HostAddress"]["IPV4"].getValue().toString();
    return ip;
}

QString QQtHostAddressManager::getHostName()
{
    return QHostInfo::localHostName();
}

QString QQtHostAddressManager::getHostAddress()
{
#if defined(__ANDROID__)
    QQtDictionaryListIterator itor ( mDictAddress["summary"].getList() );
    while ( itor.hasNext() )
    {
        const QQtDictionary& dict = itor.next();
        QString str = dict.getValue().toString();
        if ( str.contains ( "IPV4" ) )
        {
            QStringList strlist = str.split ( ',' );
            if ( strlist.size() < 2 )
                continue;
            QString ip = strlist[1];
            if ( ip.contains ( "127.0.0.1" ) )
                continue;
            return ip;
        }
    }
#elif defined(__LINUX__)
    QQtDictionaryListIterator itor ( mDictAddress["summary"].getList() );
    while ( itor.hasNext() )
    {
        const QQtDictionary& dict = itor.next();
        QString str = dict.getValue().toString();
        if ( str.contains ( "IPV4" ) )
        {
            QStringList strlist = str.split ( ',' );
            if ( strlist.size() < 2 )
                continue;
            QString ip = strlist[1];
            if ( ip.contains ( "127.0.0.1" ) )
                continue;
            return ip;
        }
    }
#endif
    QString ip = mDictAddress["host"]["HostAddress"]["IPV4"].getValue().toString();
    return ip;
}

QHostAddress QQtHostAddressManager::getBroadcast()
{
#if 0 // defined(__LINUX__)
    QQtDictionaryListIterator itor ( mDictAddress["summary"].getList() );
    while ( itor.hasNext() )
    {
        const QQtDictionary& dict = itor.next();
        QString str = dict.getValue().toString();
        if ( str.contains ( "IPV4" ) )
        {
            QStringList strlist = str.split ( ',' );
            if ( strlist.size() < 2 )
                continue;
            QString ip = strlist[1];
            if ( ip.contains ( "127.0.0.1" ) )
                continue;
            QStringList ipList = ip.split ( '.' );
            ipList[3] = "255";
            ip = ipList.join ( '.' );
            return QHostAddress ( ip );
        }
    }
#endif
    return QHostAddress ( QHostAddress::Broadcast );
}
